package com.hw.paltfrom.service.banlist;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.mapper.HwBlacklistDriverDetailsMapper;
import com.hw.paltfrom.mapper.HwBlacklistDriverMapper;
import com.hw.paltfrom.pojo.HwBlacklistDriver;
import com.hw.paltfrom.pojo.HwBlacklistDriverDetails;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.MapRemoveNullUtil;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.BlacklistDriverDetailsVO;
import com.hw.paltfrom.vo.BlacklistDriverVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 驾驶员黑名单管理
 * 梁天
 * 2019/11/06
 *
 **/
@Service
public class BlacklistDriverServer {

    @Autowired
    private HwBlacklistDriverMapper hwBlacklistDriverMapper;
    @Autowired
    private HwBlacklistDriverDetailsMapper hwBlacklistDriverDetailsMapper;


    public Map<String, Object> queryAllList(HashMap<String, Object> vo) {

        int pageNum = Integer.parseInt(vo.get("pageNum").toString());
        int pageSize = Integer.parseInt(vo.get("pageSize").toString());
        PageHelper.startPage(pageNum, pageSize);

        vo.remove("pageNum");
        vo.remove("pageSize");
        MapRemoveNullUtil.removeNullEntry(vo);

        Wrapper<HwBlacklistDriver> namewrapper = new EntityWrapper<>();

        namewrapper.allEq(vo).orderBy("F_CreateDate",false);

        List<HwBlacklistDriver> hwBlacklistDriverList = hwBlacklistDriverMapper.selectList(namewrapper);
        PageInfo<HwBlacklistDriver> info = new PageInfo<>(hwBlacklistDriverList);
        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
    }

    public Map<String, Object> insert(BlacklistDriverVO vo) {

        HwBlacklistDriver hwBlacklistDriver = new HwBlacklistDriver();
        hwBlacklistDriver.setfId(RandomNumberUtil.getNumber());
        hwBlacklistDriver.setJsy(vo.getJsy());
        hwBlacklistDriver.setCyzgzh(vo.getCyzgzh());
        hwBlacklistDriver.setTel(vo.getTel());
        hwBlacklistDriver.setAddress(vo.getAddress());
        hwBlacklistDriver.setSex(vo.getSex());
        hwBlacklistDriver.setItype(vo.getItype());
        hwBlacklistDriver.setTransferstate(vo.getTransferstate());
        hwBlacklistDriver.setTransferfile(vo.getTransferfile());
        hwBlacklistDriver.setTransferdatetime(new Timestamp(vo.getTransferdatetime()));
        hwBlacklistDriver.setTransferuserid(vo.getTransferuserid());
        hwBlacklistDriver.setTransferusername(vo.getTransferusername());
        hwBlacklistDriver.setfCreatedate(new Timestamp(System.currentTimeMillis()));
        hwBlacklistDriver.setfCreateuserid(vo.getfCreateuserid());
        hwBlacklistDriver.setfCreateusername(vo.getfCreateusername());
        hwBlacklistDriver.setCreatetime(new Timestamp(vo.getCreatetime()));

        Integer insert = hwBlacklistDriverMapper.insert(hwBlacklistDriver);
        if (insert != 1) {
            return ResponeResultUtil.fail(HWSystemCode.FAIL);
        }
        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }


    public Map<String, Object> update(BlacklistDriverVO vo) {

        // 通过ID查询待更新的数据
        Wrapper<HwBlacklistDriver> wrapper = new EntityWrapper<>();
        wrapper.eq("F_Id", vo.getfId());

        HwBlacklistDriver hwBlacklistDriver = new HwBlacklistDriver();
        hwBlacklistDriver.setTransferstate(vo.getTransferstate());
        hwBlacklistDriver.setTransferfile(vo.getTransferfile());
        hwBlacklistDriver.setTransferdatetime(new Timestamp( vo.getTransferdatetime()));
        hwBlacklistDriver.setTransferuserid(vo.getTransferuserid());
        hwBlacklistDriver.setTransferusername(vo.getTransferusername());

        Integer update = hwBlacklistDriverMapper.update(hwBlacklistDriver, wrapper);
        if (update != 1) {
            return ResponeResultUtil.fail(HWSystemCode.FAIL);
        }
        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }



    /**
     * 详情操作
     * @param vo
     * @return
     */
    public Map<String, Object> queryDetailsList(HashMap<String, Object> vo) {

        int pageNum = Integer.parseInt(vo.get("pageNum").toString());
        int pageSize = Integer.parseInt(vo.get("pageSize").toString());
        PageHelper.startPage(pageNum, pageSize);

        vo.remove("pageNum");
        vo.remove("pageSize");
        MapRemoveNullUtil.removeNullEntry(vo);

        Wrapper<HwBlacklistDriverDetails> namewrapper = new EntityWrapper<>();

        namewrapper.allEq(vo);

        List<HwBlacklistDriverDetails> hwBlacklistDriverDetailsList = hwBlacklistDriverDetailsMapper.selectList(namewrapper);
        PageInfo<HwBlacklistDriverDetails> info = new PageInfo<>(hwBlacklistDriverDetailsList);
        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
    }


    public Map<String, Object> insertDetails(BlacklistDriverDetailsVO vo) {
        //判断是否存在该驾驶员
        Wrapper<HwBlacklistDriver> namewrapper = new EntityWrapper<>();
        namewrapper.eq("F_Id", vo.getfDriverid());
        List<HwBlacklistDriver> selectList = hwBlacklistDriverMapper.selectList(namewrapper);
        if (selectList.isEmpty()) {
            return ResponeResultUtil.fail(HWSystemCode.DRIVER_NOT_EXIT);
        }

        HwBlacklistDriverDetails hwBlacklistDriverDetails = new HwBlacklistDriverDetails();
        hwBlacklistDriverDetails.setfId(RandomNumberUtil.getNumber());
        hwBlacklistDriverDetails.setfDriverid(vo.getfDriverid());
        hwBlacklistDriverDetails.setCph(vo.getCph());
        hwBlacklistDriverDetails.setfOverrunid(vo.getfOverrunid());
        hwBlacklistDriverDetails.setfOverrunname(vo.getfOverrunname());
        hwBlacklistDriverDetails.setjJcsj(vo.getjJcsj());
        hwBlacklistDriverDetails.setCz(vo.getCz());
        hwBlacklistDriverDetails.setCxs(vo.getCxs());
        hwBlacklistDriverDetails.setJf(vo.getJf());
        hwBlacklistDriverDetails.setFk(vo.getFk());
        hwBlacklistDriverDetails.setTransferdatetime(new Timestamp(System.currentTimeMillis()));
        hwBlacklistDriverDetails.setfZfid(vo.getfZfid());

        Integer insert = hwBlacklistDriverDetailsMapper.insert(hwBlacklistDriverDetails);
        if (insert != 1) {
            return ResponeResultUtil.fail(HWSystemCode.FAIL);
        }
        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }
}
